<?php

/**
 * Created by PhpStorm.
 * User: feng
 * Date: 2016/12/30
 * Time: 上午11:07
 */
class Lyw_cookie_login_model extends CI_Model
{
    public function __construct()
    {
        $this->load->database();
    }

    public function add($user_id, $username, $token, $order, $cookie_vaild_time)
    {
        $sql = 'insert into lyw_cookie_login (user_id , username , token ,c_order ,creat_time, vaild_time) VALUE (?,?,?,?,?,?)';
        $this->db->query($sql, array($user_id, $username, $token, $order, time(), time() + $cookie_vaild_time));
    }

    public function getOneListInfoByUsername($username)
    {
        if (!empty($username)) {
            $sql = 'select * from lyw_cookie_login where username = ?';
            $result = $this->db->query($sql, array($username))->result_array();
            return $result[0];
        } else {
            return array();
        }
    }

    public function getOneListInfoByUserId($user_id){
        if (!empty($user_id)) {
            $sql = 'select * from lyw_cookie_login where user_id = ?';
            $result = $this->db->query($sql, array($user_id))->result_array();
            return $result[0];
        } else {
            return array();
        }
    }
    public function changeToken($id, $token, $cookie_vaild_time)
    {
        $sql = 'update lyw_cookie_login set token = ? , vaild_time = ?  where user_id = ?';
        $this->db->query($sql, array($token, time() + $cookie_vaild_time, $id));
        return true;
    }

    public function changeOrder($user_id, $order, $cookie_vaild_time)
    {
        $sql = 'update lyw_cookie_login set c_order = ? , vaild_time = ? where user_id = ?';
        $this->db->query($sql, array($order, time() + $cookie_vaild_time, $user_id));
        return true;
    }

    public function had($user_id)
    {
        $sql = 'select count(*) as count from lyw_cookie_login where user_id = ?';
        $row = $this->db->query($sql, array($user_id))->row();
        if ($row->count >= 1) {
            return true;
        } else {
            return false;
        }
    }
}